//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
;***
;ulldiv.asm - unsigned long divide routine
;
;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
;
;Purpose:
;   defines the unsigned long divide routine
;       __aulldiv
;******************************************************************************

;***
;ulldiv - unsigned long divide
;
;Purpose:
;   Does a unsigned long divide of the arguments.  Arguments are
;   not changed.
;
;Entry:
;   Arguments are passed on the stack:
;       1st pushed: divisor (QWORD)
;       2nd pushed: dividend (QWORD)
;
;Exit:
;   EDX:EAX contains the quotient (dividend/divisor)
;   NOTE: this routine removes the parameters from the stack.
;
;Uses:
;   ECX
;
;Exceptions:
;
;*****************************************************************************/

//==========================================================================
// Copyright (c) 2000-2004,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
//
// The original program was copied from Microsoft Visual Studio 6.0
// Enterprise Edition.  This file is adapted for Zee operating system.
//==========================================================================

#include <elatypes.h>

__declspec(naked) void __cdecl _aulldiv(void)
{
    __asm {
    push    ebx
    push    esi

; Set up the local stack and save the index registers.  When this is done
; the stack frame will look as follows (assuming that the expression a/b will
; generate a call to uldiv(a, b)):
;
;           -----------------
;           |               |
;           |---------------|
;           |               |
;           |--divisor (b)--|
;           |               |
;           |---------------|
;           |               |
;           |--dividend (a)-|
;           |               |
;           |---------------|
;           | return addr** |
;           |---------------|
;           |      EBX      |
;           |---------------|
;   ESP---->|      ESI      |
;           -----------------
;

#define DVNDLO  [esp + 12]  /* stack address of dividend (a) loword */
#define DVNDHI  [esp + 16]  /* stack address of dividend (a) hiword */
#define DVSRLO  [esp + 20]  /* stack address of divisor (b) loword */
#define DVSRHI  [esp + 24]  /* stack address of divisor (b) hiword */

;
; Now do the divide.  First look to see if the divisor is less than 4194304K.
; If so, then we can use a simple algorithm with word divides, otherwise
; things get a little more complex.
;

    mov eax,DVSRHI                  ; check to see if divisor < 4194304K
    or  eax,eax
    jnz short L1                    ; nope, gotta do this the hard way
    mov ecx,DVSRLO                  ; load divisor
    mov eax,DVNDHI                  ; load high word of dividend
    xor edx,edx
    div ecx                         ; get high order bits of quotient
    mov ebx,eax                     ; save high bits of quotient
    mov eax,DVNDLO                  ; edx:eax <- remainder:lo word of dividend
    div ecx                         ; get low order bits of quotient
    mov edx,ebx                     ; edx:eax <- quotient hi:quotient lo
    jmp short L2                    ; restore stack and return

;
; Here we do it the hard way.  Remember, eax contains DVSRHI
;

L1:
    mov ecx,eax                     ; ecx:ebx <- divisor
    mov ebx,DVSRLO
    mov edx,DVNDHI                  ; edx:eax <- dividend
    mov eax,DVNDLO
L3:
    shr ecx,1                       ; shift divisor right one bit; hi bit <- 0
    rcr ebx,1
    shr edx,1                       ; shift dividend right one bit; hi bit <- 0
    rcr eax,1
    or  ecx,ecx
    jnz short L3                    ; loop until divisor < 4194304K
    div ebx                         ; now divide, ignore remainder
    mov esi,eax                     ; save quotient

;
; We may be off by one, so to check, we will multiply the quotient
; by the divisor and check the result against the orignal dividend
; Note that we must also check for overflow, which can occur if the
; dividend is close to 2**64 and the quotient is off by 1.
;

    mul dword ptr DVSRHI            ; QUOT * DVSRHI
    mov ecx,eax
    mov eax,DVSRLO
    mul esi                         ; QUOT * DVSRLO
    add edx,ecx                     ; EDX:EAX = QUOT * DVSR
    jc  short L4                    ; carry means Quotient is off by 1

;
; do long compare here between original dividend and the result of the
; multiply in edx:eax.  If original is larger or equal, we are ok, otherwise
; subtract one (1) from the quotient.
;

    cmp edx,DVNDHI                  ; compare hi words of result and original
    ja  short L4                    ; if result > original, do subtract
    jb  short L5                    ; if result < original, we are ok
    cmp eax,DVNDLO                  ; hi words are equal, compare lo words
    jbe short L5                    ; if less or equal we are ok, else subtract
L4:
    dec esi                         ; subtract 1 from quotient
L5:
    xor edx,edx                     ; edx:eax <- quotient
    mov eax,esi

;
; Just the cleanup left to do.  edx:eax contains the quotient.
; Restore the saved registers and return.
;

L2:

    pop esi
    pop ebx

    ret 16
    }
}

void __cdecl _aulldvrm(void)
{
    _aulldiv();
}

__declspec(naked) void __cdecl _alldvrm()
{
    /* *INDENT-OFF* */
    __asm {
        push        edi
        push        esi
        push        ebx
        xor         edi,edi
        mov         eax,dword ptr [esp+14h]
        or          eax,eax
        jge         L1
        inc         edi
        mov         edx,dword ptr [esp+10h]
        neg         eax
        neg         edx
        sbb         eax,0
        mov         dword ptr [esp+14h],eax
        mov         dword ptr [esp+10h],edx
L1:
        mov         eax,dword ptr [esp+1Ch]
        or          eax,eax
        jge         L2
        inc         edi
        mov         edx,dword ptr [esp+18h]
        neg         eax
        neg         edx
        sbb         eax,0
        mov         dword ptr [esp+1Ch],eax
        mov         dword ptr [esp+18h],edx
L2:
        or          eax,eax
        jne         L3
        mov         ecx,dword ptr [esp+18h]
        mov         eax,dword ptr [esp+14h]
        xor         edx,edx
        div         ecx
        mov         ebx,eax
        mov         eax,dword ptr [esp+10h]
        div         ecx
        mov         edx,ebx
        jmp         L4
L3:
        mov         ebx,eax
        mov         ecx,dword ptr [esp+18h]
        mov         edx,dword ptr [esp+14h]
        mov         eax,dword ptr [esp+10h]
L5:
        shr         ebx,1
        rcr         ecx,1
        shr         edx,1
        rcr         eax,1
        or          ebx,ebx
        jne         L5
        div         ecx
        mov         esi,eax
        mul         dword ptr [esp+1Ch]
        mov         ecx,eax
        mov         eax,dword ptr [esp+18h]
        mul         esi
        add         edx,ecx
        jb          L6
        cmp         edx,dword ptr [esp+14h]
        ja          L6
        jb          L7
        cmp         eax,dword ptr [esp+10h]
        jbe         L7
L6:
        dec         esi
L7:
        xor         edx,edx
        mov         eax,esi
L4:
        dec         edi
        jne         L8
        neg         edx
        neg         eax
        sbb         edx,0
L8:
        pop         ebx
        pop         esi
        pop         edi
        ret         10h
    }
    /* *INDENT-ON* */
}
